package com.yogee.topseller.fragment.home;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.IntelligentScreeningListRvAdapter;
import com.yogee.topseller.adapter.PayCalendarPgAdapter;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.RemitCalendarBean;
import com.yogee.topseller.http.HttpManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/7 0007 9:54
 * 创建人：wangye
 * 修改备注：
 */

public class PayCalendarFragment extends HttpFragment implements ViewPager.OnPageChangeListener {
    @BindView(R.id.calendar_date)
    TextView calendarDate;
    @BindView(R.id.calendar_recycler)
    RecyclerView calendarRecycler;
    @BindView(R.id.calendar_linear)
    ViewPager calendarVp;
    private IntelligentScreeningListRvAdapter listAdapter;
    private List<InterlligentFilterBean.ProductListBean> productListBeen = new ArrayList<>();
    private List<RemitCalendarBean.CalendarListBean> calendarListBeen = new ArrayList<>();
    private PayCalendarPgAdapter calendarPgAdapter;
    private Map<Integer, List<RemitCalendarBean.CalendarListBean>> map;
    private Map<Integer, List<String>> seleMap;
    private String time;
    private int position = -1, nowDate;
    private DateFormat format2;
    private Date date;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pay_calender;
    }

    @Override
    protected void initView() {
        remitCalendar();
        //获取当前时间
        date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format2 = new SimpleDateFormat("yyyy年MM月");
        time = format.format(date);

        //显示当前时间的列表
        remitCalendarProductList(time);

        calendarPgAdapter = new PayCalendarPgAdapter(getActivity());
        calendarVp.setAdapter(calendarPgAdapter);
        calendarVp.setOnPageChangeListener(this);

        calendarPgAdapter.setDateClick(new PayCalendarPgAdapter.dateClick() {
            @Override
            public void click(String date, String topDate) {
                calendarDate.setText(topDate);
                remitCalendarProductList(date);
            }
        });

        listAdapter = new IntelligentScreeningListRvAdapter(getActivity());
        calendarRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        calendarRecycler.setAdapter(listAdapter);
    }

    //打款日历日历
    private void remitCalendar() {
        HttpManager.getInstance().remitCalendar()
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RemitCalendarBean>() {
            @Override
            public void onNext(RemitCalendarBean o) {
                PayCalendarFragment.this.loadingFinished();
                calendarListBeen = o.getCalendarList();

                map = new HashMap<Integer, List<RemitCalendarBean.CalendarListBean>>();
                seleMap = new HashMap<Integer, List<String>>();

                int j = calendarListBeen.size() / 7;
                for (int i = 0; i < j; i++) {
                    map.put(i, calendarListBeen.subList(i * 7, i * 7 + 7));
                    List<String> click = new ArrayList<>();
                    for (int k = 0; k < 7; k++) {
                        click.add("0");
                    }
                    seleMap.put(i, click);
                }
                for (int i = 0; i < map.size(); i++) {
                    for (int k = 0; k < map.get(i).size(); k++) {
                        if (map.get(i).get(k).getReturnDate().equals(time)) {
                            position = i;
                            nowDate = k;
                            seleMap.get(i).set(k, "1");
                            break;
                        }
                    }
                }
                calendarPgAdapter.addData(map, seleMap);
                if (position == -1) {
                    calendarDate.setText(map.get(map.size() - 1).get(0).getTopDate());
                    calendarVp.setCurrentItem(map.size() - 1);
                } else {
                    calendarPgAdapter.setNowBg(position, nowDate);
                    calendarDate.setText(format2.format(date));
                    calendarVp.setCurrentItem(position);
                }

            }
        }, this));
    }

    //打款日历列表
    private void remitCalendarProductList(String date) {
        HttpManager.getInstance().remitCalendarProductList(date)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
            @Override
            public void onNext(InterlligentFilterBean o) {
                PayCalendarFragment.this.loadingFinished();
                productListBeen = o.getProductList();
                listAdapter.addData(productListBeen);
            }
        }, this));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position != this.position)
            calendarDate.setText(map.get(position).get(0).getTopDate());
        else
            calendarDate.setText(format2.format(date));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
